import dic from "./Dictionary";

class Language {
  constructor(lang) {
    if (lang) {
      this.lang = lang;
    } else {
      this.lang =
        window.localStorage.getItem("language") ||
        navigator.language ||
        navigator.userLanguage;
    }
  }
  getEn = function (str = "") {
    if (!str) {
      return str;
    }
    if (/^[a-zA-Z0-9\s]+$/.test(str)) {
      return str;
    }
    // const keys = Object.keys(dic);
    // return keys.includes(str) ? dic[str] : str;
    return Object.keys(dic).find((key) => dic[key] === str) || str;
  };

  getZh = function (str = "") {
    if (/^[\u4e00-\u9fa5\s0-9]{0,}$/.test(str)) {
      return str;
    } else {
      let zhStr = dic[str] || str;
      return zhStr;
    }
  };
  local = (str = "") => {
    return this.lang == "zh-CN" ? this.getZh(str) : this.getEn(str);
  };
  // static install(Vue) {
  //   Vue.prototype.$language = new Language();
  // }
}

export default new Language();
